<template>
  <el-dialog :visible="visible" @close="$emit('update:visible', false)">
    <el-form ref="formData" label-width="120px" :model="addForm" :rules="rules">
      <el-form-item prop="name" label="部门名称">
        <el-input
          v-model="addForm.name"
          placeholder="2-10个字符"
          style="width: 80%"
          size="mini"
        />
      </el-form-item>

      <el-form-item prop="code" label="部门编码">
        <el-input
          v-model="addForm.code"
          placeholder="2-10个字符"
          style="width: 80%"
          size="mini"
        />
      </el-form-item>

      <el-form-item prop="introduce" label="部门负责人">
        <el-select
          v-model="addForm.introduce"
          placeholder="请选择负责人"
          style="width: 80%"
          size="mini"
        >
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.id" />
        </el-select>
      </el-form-item>

      <el-form-item prop="managerId" label="部门介绍">
        <el-input
          v-model="addForm.managerId"
          placeholder="1-100个字符"
          type="textarea"
          size="mini"
          :rows="4"
          style="width: 80%"
        />
      </el-form-item>

      <el-form-item>
        <!-- 按钮 -->
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button
              size="mini"
              type="primary"
              @click="onSubmit"
            >确定</el-button>

            <el-button
              size="mini"
              @click="$emit('update:visible', false)"
            >取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addDepartmentAPI, getDepartmentAPI, getDepartmentDetailAPI, getManagerListAPI, updateDepartmentAPI } from '@/api/department'

export default {
  props: {
    visible: {
      type: Boolean
    },
    currentId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      managerList: [],
      addForm: {
        code: '',
        introduce: '',
        managerId: '',
        name: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '长度在 2 到 10 个字符',
            trigger: 'blur'
          },
          {
            trigger: 'blur',
            validator: async(rule, value, callback) => {
              let res = await getDepartmentAPI()
              if (this.addForm.id) {
                res = res.filter(v => v.id !== this.addForm.id)
              }
              const nameList = res.map((v) => v.name)
              if (nameList.includes(value)) {
                callback(new Error('部门名称已存在'))
              } else {
                callback()
              }
            }
          }
        ],
        code: [
          { required: true, message: '', trigger: 'blur' },
          {
            min: 2,
            max: 10,
            message: '长度在 2 到 10 个字符',
            trigger: 'blur'
          },
          {
            trigger: 'blur',
            validator: async(rule, value, callback) => {
              let res = await getDepartmentAPI()
              if (this.addForm.id) {
                res = res.filter(v => v.id !== this.addForm.id)
              }
              const codeList = res.map((v) => v.code.toUpperCase())
              if (codeList.includes(value.toUpperCase())) {
                callback(new Error('部门编码已存在'))
              } else {
                callback()
              }
            }
          }
        ],
        introduce: [
          { required: true, message: '请选择负责人', trigger: 'blur' }
        ],
        managerId: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' }, {
          min: 1, max: 100, message: '部门介绍的长度为1-100个字符', trigger: 'blur'
        }]
      }
    }
  },
  watch: {
    visible(newValue) {
      if (!newValue) {
        this.$refs.formData.resetFields()
        this.addForm = {
          code: '',
          introduce: '',
          managerId: '',
          name: ''
        }
      }
    }
  },
  created() {
    this.getManagerList()
  },
  methods: {
    async  onSubmit() {
      this.$refs.formData.validate()
      !this.addForm.id ? await addDepartmentAPI({
        ...this.addForm,
        pid: this.currentId
      })
        : await updateDepartmentAPI(this.addForm)
      this.$message.success(this.addForm.id ? '添加成功' : '编辑成功')
      this.$emit('update:visible', false)
      this.$emit('refresh')
    },
    async  getManagerList() {
      const res = await getManagerListAPI()
      this.managerList = res
    },
    async getDepDetail(id) {
      const res = await getDepartmentDetailAPI(id)
      this.addForm = res
    }
  }

}
</script>

<style scoped></style>
